package butines.core;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;

import javax.swing.SwingUtilities;

public class VideoManager {

    private GraphicsDevice graphicsDevice;
    
    private DisplayMode desktop;
    
    private Cursor emptyCursor;
    private Cursor defaultCursor;
    private boolean cursorVisible = true;
        
    private boolean fullScreen;
    private int width;
    private int height;
    
    public VideoManager(GraphicsDevice graphicsDevice) {
        this.graphicsDevice = graphicsDevice;
        this.desktop = graphicsDevice.getDisplayMode();
        
        if (!GraphicsEnvironment.isHeadless() && emptyCursor == null) {
            final Toolkit toolkit = Toolkit.getDefaultToolkit();
            final Image emptyImage = toolkit.getImage("");
            emptyCursor = toolkit.createCustomCursor(emptyImage, new Point(0, 0), "invisible");
            defaultCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
        }
        
    }

    public void init(Window window) {
        window.setFont(new Font("Dialog", Font.PLAIN, 14));
        window.setBackground(Color.black);
        window.setForeground(Color.white);
    }
    
    public void shutdown(Window window) {
        window.dispose();
        if (fullScreen) {
            graphicsDevice.setFullScreenWindow(null);
            fullScreen = false;
        }
    }

    public void setCursorVisible(Window window, boolean visible) {
        if (!GraphicsEnvironment.isHeadless()) {
            window.setCursor(visible ? defaultCursor : emptyCursor);
            cursorVisible = visible;
        }
    }
    
    public boolean isCursorVisible() {
        return cursorVisible;
    }
    

    public boolean isFullScreen() {
        return fullScreen;
    }
    
    private void createBufferStrategy(final Window window) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                window.createBufferStrategy(2);
            }
        });
    }
    
    public void setFullScreen(Window window, DisplayMode displayMode) {
        
        if (graphicsDevice.isFullScreenSupported()) {

            if (!fullScreen) {
                graphicsDevice.setFullScreenWindow(window);
            }
            
            if (graphicsDevice.isDisplayChangeSupported() && displayMode != null) {
                graphicsDevice.setDisplayMode(displayMode);
                width = displayMode.getWidth();
                height = displayMode.getHeight();
            }
            
            createBufferStrategy(window);
                        
            fullScreen = true;
        }
        
    }

    public void setFullScreen(Window window, int width, int height) {
        DisplayMode displayMode = new DisplayMode(width, height, desktop.getBitDepth(), desktop.getRefreshRate());
        setFullScreen(window, displayMode);
    }
    
   public void setWindow(Window window, int w, int h) {

       graphicsDevice.setFullScreenWindow(null);
       
       int left = (desktop.getWidth() - w) / 2;
       int top = (desktop.getHeight() - h) / 2;

       window.setLocation(left, top);
       window.setSize(w, h);
       window.setVisible(true);
       createBufferStrategy(window);
       window.requestFocus();
      
       width = w;
       height = h;
       fullScreen = false;
   }

    public int getWidth() {
       return width;
    }
    
    public int getHeight() {
       return height;
    }

}
